//
//  FirstDetailControllerViewController.m
//  LoveListen
//
//  Created by 王喆 on 15/7/1.
//  Copyright (c) 2015年 王喆. All rights reserved.
//

#import "FirstDetailControllerViewController.h"
#import "FirstDetailModel.h"
#import "FirstDetailCell.h"
#import "DetailViewController.h"
#import "UIImageView+WebCache.h"
#import "DescriptionViewController.h"
#define kurl @"http://app.9nali.com/935/bozhus/%@?page_id=1&device=iPhone&version=1.1.5"
@interface FirstDetailControllerViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation FirstDetailControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatTableView];
    [self addTaskWithUrl:[NSString stringWithFormat:kurl,self.uid]];
    [self addButton];
}

-(void)creatTableView{
    self.dataArr = [[NSMutableArray alloc]init];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0,kScreenSize.width , kScreenSize.height) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 50;
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kScreenSize.width, 300)];
    
    
    self.tableView.tableHeaderView = self.imageView;
    
    
    
    [self.tableView registerNib:[UINib nibWithNibName:@"FirstDetailCell" bundle:nil] forCellReuseIdentifier:@"Detail"];
    [self.view addSubview:self.tableView];
    
    
}


-(void)addTaskWithUrl:(NSString *)url{
    
    _manager =[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [_manager GET:url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"下载成功");
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                NSMutableArray *arr = dict[@"list"];
        for (NSDictionary *dict2 in arr) {
            FirstDetailModel *model = [[FirstDetailModel alloc] init];
            [model setValuesForKeysWithDictionary:dict2];
            [model setValuesForKeysWithDictionary:dict];
            
            
            
            self.largeLogo = model.largeLogo;
            self.navigationItem.title = model.title;
            self.personalSignature = model.personalSignature;
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:self.largeLogo] placeholderImage:[UIImage imageNamed:@"me"]];
            [self.dataArr addObject:model];
            
        }
        
        [self.tableView reloadData];
        
        
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"下载失败");
        
    }];
    
    
}


#pragma mark - jianjie
-(void)addButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(self.tableView.tableHeaderView.frame.size.width-50,self.tableView.tableHeaderView.frame.size.height-50 , 30, 30) ;
    [button setBackgroundImage:[UIImage imageNamed:@"btn_user_info_n"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.tableView.tableHeaderView.userInteractionEnabled = YES;
    [self.tableView.tableHeaderView addSubview:button];
    
    
}
-(void)btnClick:(UIButton *)button {
    DescriptionViewController *descti = [[DescriptionViewController alloc] init];
    descti.personalSignature = self.personalSignature;
    [self.navigationController pushViewController:descti animated:YES];
    
    
}


#pragma mark UITableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArr.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    FirstDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Detail" forIndexPath:indexPath];
    FirstDetailModel *model = self.dataArr[indexPath.row];
    [cell showDataWithModel:model indexPath:indexPath];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailViewController *detail = [[DetailViewController alloc]init];
    FirstDetailModel *model = self.dataArr[indexPath.row];
    detail.albumId = model.albumId;
    
    
    
    [self.navigationController pushViewController:detail animated:YES];
    
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end